package com.gong.servlet;

import com.alibaba.fastjson.JSONArray;
import com.gong.constants.UserConstants;
import com.gong.pojo.Role;
import com.gong.pojo.User;
import com.gong.service.role.RoleService;
import com.gong.service.role.RoleServiceImpl;
import com.gong.service.user.UserService;
import com.gong.service.user.UserServiceImpl;
import com.gong.utils.PageSupport;
import com.mysql.cj.util.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: UserServlet
 * @Description: TODO
 * @Author: 。。。
 * @Data: 2021/10/2016:26
 * @Version: 1.0
 **/
public class UserServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String method = req.getParameter("method");
		if (method != null && method.equals("savepwd")) {
			this.updatePassword(req, resp);
		} else if (method != null && method.equals("pwdmodify")) {
			this.pwdmodify(req, resp);
		} else if (method != null && method.equals("query")) {
			this.query(req, resp);
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}

	public void updatePassword(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		Object user = req.getSession().getAttribute(UserConstants.USER_SESSION);

		String newpassword = req.getParameter("newpassword");
		String renewpassword = req.getParameter("rnewpassword");

		boolean flag = false;

		if (user != null && !StringUtils.isNullOrEmpty(newpassword) && newpassword.equals(renewpassword)) {
			UserService userService = new UserServiceImpl();

			flag = userService.updateUserPassword(((User) user).getId(), newpassword);

			if (flag) {
				req.setAttribute("message", "密码修改成功请重新登录");
				req.getSession().removeAttribute(UserConstants.USER_SESSION);
			} else {
				req.setAttribute("message", "密码修改错误");
				resp.sendRedirect("pwdmodify.jsp");
			}
		} else {
			req.setAttribute("message", "密码错误");
		}
		req.getRequestDispatcher("pwdmodify.jsp").forward(req, resp);
	}

	public void pwdmodify(HttpServletRequest req, HttpServletResponse resp) {
		Object o = req.getSession().getAttribute(UserConstants.USER_SESSION);
		String oldpassword = req.getParameter("oldpassword");

		Map<String, String> map = new HashMap<>();

		if (o == null) {
			//session过期
			map.put("result", "sessionerror");
		} else if (StringUtils.isNullOrEmpty(oldpassword)) {
			map.put("result", "error");
		} else {
			String userpassword = ((User) o).getUserpassword();

			if (userpassword.equals(oldpassword)) {
				map.put("result", "true");
			} else {
				map.put("result", "false");
			}
		}

		try {
			resp.setContentType("application/json");
			PrintWriter writer = resp.getWriter();
			writer.write(JSONArray.toJSONString(map));
			writer.flush();
			writer.close();

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void query(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String queryName = req.getParameter("queryName");
		String temp = req.getParameter("queryUserRole");
		String pageIndex = req.getParameter("pageIndex");
		String inputPageTemp = req.getParameter("inputPage");

		int inputPage = 0;

		int queryUserRole = 0;

		UserService userService = new UserServiceImpl();
		RoleService roleService = new RoleServiceImpl();
		List<Role> roleList = null;
		List<User> userList = null;

		int pageSize = 5;
		int currentPageNo = 1;

		//if (queryName == null) {
		//	queryName = "";
		//}
		if (temp != null && !temp.equals("")) {
			queryUserRole = Integer.parseInt(temp);
		}
		if (pageIndex != null) {
			try {
				Integer integer = Integer.valueOf(pageIndex);
			} catch (NumberFormatException e) {
				e.printStackTrace();
			}
		}

		int totalCount = userService.getUserCount(queryName, queryUserRole);

		PageSupport page = new PageSupport();

		page.setPageSize(pageSize);
		page.setCurrentPageNo(currentPageNo);
		page.setTotalCount(totalCount);

		int totalPageCount = page.getTotalPageCount();
		//控制首页和尾页
		if (currentPageNo < 1) {
			currentPageNo = 1;
		} else if (currentPageNo > totalPageCount) {
			currentPageNo = totalPageCount;
		}

		if (inputPageTemp != null) {
			if (Integer.parseInt(inputPageTemp) > totalPageCount) {
				currentPageNo = 1;
			}
		}

		userList = userService.getUserList(queryName, queryUserRole, currentPageNo, pageSize);
		roleList = roleService.getUserRole();

		req.setAttribute("userList", userList);
		req.setAttribute("queryUserName", queryName);
		req.setAttribute("queryUserRole", queryUserRole);
		req.setAttribute("totalPageCount", totalPageCount);
		req.setAttribute("totalCount", totalCount);
		req.setAttribute("roleList", roleList);
		req.setAttribute("currentPageNo", currentPageNo);

		req.getRequestDispatcher("userlist.jsp").forward(req, resp);
	}
}
